<?php

namespace App\Http\Controllers\Home;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;

use App\Http\Model\Goods;
use App\Http\Model\Type;

use DB;

class SearchclickController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        

          //实例化商品详情表
        $goods = new Goods();

        //获取uid(从session);
         $uid = session('huser')->id;

         
        //通过传递过来的商品的ID值来获取gid
        $gid = $id;

        //获取本个商品的收藏的数量
        $collect = DB::table('sad_collection')->where('gid',$gid)->where('uid',$uid)->first();

        if($collect['status']==0)
        {

            $status = 1;
            $res = DB::table('sad_collection')
                ->insert(['gid'=>$gid,'uid'=>$uid,'status'=>$status]);
            if($res)
            {

                    //获取收藏数量

                    //将收藏的总量添加到googs的表中
                    
                     $num = DB::table('sad_collection')
                                ->where('gid',$gid)
                                ->lists('status');
                        
                    $sum = count($num);

                    $goods = new Goods();

                    $result = $goods->where('id',$gid)->update(['count'=>$sum]);

                    if($result)
                    {

                        return back()->with('success','收藏商品成功');
                    }else
                    {

                        return back()->with('error','收藏商品失败');
                    }



                return back()->with('success','收藏成功');

            }else{

                return back()->with('error','收藏失败');
            }      
        
        
        }else
        {

            return back()->with('error','已经收藏');
        }

    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}
